//
//  HLLoginAgreeProtocolView.m
//  YueYue
//
//  Created by hulu001 on 2023/5/23.
//

#import "HLLoginAgreeProtocolView.h"

@implementation HLLoginAgreeProtocolView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupView];
        
    }
    return self;
}

- (void)setupView {
    
    self.agreeLabel = [[YYLabel alloc] init];
    _agreeLabel.font = UIFontMake(12);
    _agreeLabel.textColor = HLTextColor3;
    _agreeLabel.numberOfLines = 0;
    _agreeLabel.textAlignment = NSTextAlignmentLeft;
    
    NSString *text = @" 我已阅读并同意《用户服务协议》和《隐私协议》";
    NSMutableAttributedString *attString = [NSMutableAttributedString.alloc initWithString:text];
    [attString yy_setAttribute:NSForegroundColorAttributeName value:HLTextColor3];

    MJWeakSelf;
    [attString yy_setTextHighlightRange:[text rangeOfString:@"《用户服务协议》"] color:HLMainColor backgroundColor:_agreeLabel.backgroundColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [HLUIHelper h5WithURL:HLH5ProtocolUserUrl controller:weakSelf.qmui_viewController];
    }];
    [attString yy_setTextHighlightRange:[text rangeOfString:@"《隐私协议》"] color:HLMainColor backgroundColor:_agreeLabel.backgroundColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        [HLUIHelper h5WithURL:HLH5ProtocolPrivacyUrl controller:weakSelf.qmui_viewController];
    }];

    self.agreeButton = [[QMUIButton alloc] init];
    [_agreeButton setImage:UIImageMake(@"login_unchecked") forState:UIControlStateNormal];
    [_agreeButton setImage:UIImageMake(@"login_checked") forState:UIControlStateSelected];
    [_agreeButton addTarget:self action:@selector(agreeAction:) forControlEvents:UIControlEventTouchUpInside];
    [_agreeButton setContentEdgeInsets:UIEdgeInsetsMake(2, 2, 2, 2)];
    [self addSubview:_agreeButton];
    [_agreeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(2);
    }];
    
    _agreeLabel.attributedText = attString;
    [self addSubview:_agreeLabel];
    [_agreeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(2);
        make.left.equalTo(_agreeButton.mas_right).offset(2);
        make.bottom.right.mas_equalTo(-4);
    }];
    
    
}

- (void)agreeAction:(QMUIButton *)btn
{
    btn.selected = !btn.selected;
}

@end
